/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.smarttmt.idu.web.beans;

import com.smarttmt.idu.web.behavior.Preloadable;
import com.smarttmt.idu.web.model.UserLogin;
import com.smarttmt.idu.web.utilities.FacesUtils;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import javax.faces.bean.ManagedProperty;

/**
 *
 * @author Nicolás Rubén Caballero Ortega (helaman58@gmail.com)
 */
@Named(value = "sessionControllerBean")
@SessionScoped
public class SessionController implements Serializable {

    private String generalTitle;
    private URI templateAuthenticated;
    private URI templateNotAuthenticated;
    @ManagedProperty(value = "#{applicationControllerBean}")
    private ApplicationController applicationControllerBean;

    /**
     * Creates a new instance of SessionController
     */
    public SessionController() {
    }
    
    public String navigationURL(String navigationRule, String destinyURL, String fromURL){
        //Aqui se deberia implementar lógica para que se deje en el log la navegabilida del usuario
        writeNavigationLog(navigationRule);
        
        if(navigationRule.equals("userLogin")){
            UserLogin userLoginBean = (UserLogin) FacesUtils.getManagedBean(navigationRule+"Bean");
            if(userLoginBean.isAuthenticated()){
                navigationRule = destinyURL;
            }
        }

        try {
            Preloadable bean = (Preloadable) FacesUtils.getManagedBean(navigationRule+"Bean");
            Map<String,Object> parametersbean = new HashMap<>();
            parametersbean.put("fromURL", fromURL);
            parametersbean.put("destinyURL", destinyURL);
            bean.preload(parametersbean);
            return navigationRule;
        } catch (Exception e) {
            return null;
        }
    }

    public String navigationURL(String navigationRule) {
        //Aqui se deberia implementar lógica para que se deje en el log la navegabilida del usuario
        writeNavigationLog(navigationRule);

        try {
            Preloadable bean = (Preloadable) FacesUtils.getManagedBean(navigationRule+"Bean");
            bean.preload(null);
            return navigationRule;
        } catch (Exception e) {
            return null;
        }

    }

    public void writeNavigationLog(String navigationRule) {
        //System.out.println(navigationRule);
    }
    
    public void writeEventsLog(){
        
    }

    public String getGeneralTitle() {
        return generalTitle;
    }

    public void setGeneralTitle(String generalTitle) {
        this.generalTitle = generalTitle;
    }

    public URI getTemplateAuthenticated() {
        return templateAuthenticated;
    }

    public void setTemplateAuthenticated(URI templateAuthenticated) {
        this.templateAuthenticated = templateAuthenticated;
    }

    public URI getTemplateNotAuthenticated() {
        return templateNotAuthenticated;
    }

    public void setTemplateNotAuthenticated(URI templateNotAuthenticated) {
        this.templateNotAuthenticated = templateNotAuthenticated;
    }

    public ApplicationController getApplicationControllerBean() {
        return applicationControllerBean;
    }

    public void setApplicationControllerBean(ApplicationController applicationControllerBean) {
        this.applicationControllerBean = applicationControllerBean;
    }
    
}
